package org.bastion.domain

import org.bastion.domain.adapter.Adapter
import org.bastion.domain.message.DomainMessage

class Domain {
  
  var adapters = scala.collection.mutable.Map[String, Adapter]()
    
  def addAdapter(name: String, adapter: Adapter) {
    adapters.put(name, adapter)
  }
  
  def ![T <: DomainMessage](message: T): T = {
    println("Received message " + message)
    val adapter = adapters.get(message.label)
    println("Adapter = " + adapter)
    adapter match {
      case Some(a) => a.handle(message)
      case None => println("No adapter registered for message " + message.label)
    }
    message
  }

}

object Domain {
  
  val threadGlobal = new ThreadGlobal[Domain]
  
  def instance = {
    if (threadGlobal.value == null) {
      threadGlobal.set(new Domain)
    }
    threadGlobal.value
  }
  
}